/**********************************************************************************
**
** Copyright (C) 2009 Jostein Bratlie
** Contact: jostein.bratlie@gmail.com | http://bratlie.info | http://episteme.hin.no
**
** This file is part of GMQtExt, a GMlib and Qt extension library
**
** GMQtExt is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** GMQtExt is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with GMQtExt.  If not, see <http://www.gnu.org/licenses/>.
**
**********************************************************************************/


// Qt
#include <QtGui>

// header
#include "createobjectwidget.h"

// GMlib
#include <gmParametrics>

namespace GMQtExt {


  GMlib::PCircle<float>* CreateObjectWidget::_prototype_pcircle_float = new GMlib::PCircle<float>();
  GMlib::PSphere<float>* CreateObjectWidget::_prototype_psphere_float = new GMlib::PSphere<float>();
  GMlib::PTorus<float>* CreateObjectWidget::_prototype_ptorus_float = new GMlib::PTorus<float>();


  CreateObjectWidget::CreateObjectWidget( QWidget* parent ) : QWidget( parent ) {

    QGridLayout *layout;
    layout = new QGridLayout(this);

    _item_tree = new QTreeWidget;
    _item_tree->setHeaderHidden( true );
    _populateItemTree();

    layout->addWidget( _item_tree, 0, 0 );

    connect( _item_tree, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
             this, SLOT(_slotObjectSelected(QTreeWidgetItem*,int)) );
  }

  void CreateObjectWidget::_populateItemTree() {

    QTreeWidgetItem* pcurve;
    pcurve = createGroup( tr("Tensor Product Curves") );

    createItem( tr("Circle"), "pcircle_float", pcurve );

    QTreeWidgetItem* psurf;
    psurf = createGroup( tr("Tensor Product Surfaces") );

    createItem( tr("Sphere"), "psphere_float", psurf );
    createItem( tr("Torus"), "ptorus_float", psurf );
  }

  void CreateObjectWidget::_slotObjectSelected( QTreeWidgetItem* item, int /*column*/ ) {

    QString id = getItemId( item );

    if( id == "pcircle_float" )
      emit signObjectSelected( _prototype_pcircle_float );
    else if( id == "psphere_float" )
      emit signObjectSelected( _prototype_psphere_float );
    else if( id == "ptorus_float" )
      emit signObjectSelected( _prototype_ptorus_float );
  }

  void CreateObjectWidget::clearSelection() {

    _item_tree->setCurrentItem( 0x0 );
  }

  QTreeWidgetItem* CreateObjectWidget::createGroup( const QString &name, QTreeWidgetItem *p_group ) {

    QTreeWidgetItem *group = new QTreeWidgetItem();
    group->setText( 0, name );

    if( p_group )
      p_group->addChild( group );
    else
      _item_tree->addTopLevelItem( group );

    return group;
  }

  QTreeWidgetItem* CreateObjectWidget::createItem( const QString& name, const QString& identifier, QTreeWidgetItem *group ) {

    QTreeWidgetItem *item = new QTreeWidgetItem();
    item->setText( 0, name );

    if( group )
      group->addChild( item );

    _item_id[item] = identifier;

    return item;
  }

  QString CreateObjectWidget::getItemId( QTreeWidgetItem* item ) const {

    return _item_id.value( item, "" );
  }

  bool CreateObjectWidget::insertObject() {

    GMlib::SceneObject* obj = 0x0;
    emit signCreateObject( &obj );

    if( !obj )
      return false;

    GMlib::PCurve<float> *pcurve = dynamic_cast<GMlib::PCurve<float>*>( obj );
    GMlib::PSurf<float> *psurf = dynamic_cast<GMlib::PSurf<float>*>( obj );
    if( pcurve )
      pcurve->replot( 100 );
    if( psurf )
      psurf->replot( 100, 100 );

    emit signInsertObject( obj );

    return true;
  }


} // END namespace GMQtExt
